package org.chronologger.poppaea.server.domain;

import java.io.Serializable;

import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import org.chronologger.poppaea.client.domain.Location;
import org.chronologger.poppaea.client.domain.NotSupportedOnClientException;
import org.chronologger.poppaea.client.domain.Scene;

import com.google.appengine.api.datastore.Text;

@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class PersistentScene implements Serializable {

	private static final long serialVersionUID = 1L;

	@PrimaryKey
	@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
	private Long id;

	@Persistent
	private String title;
	@Persistent
	private Text notes;
	@Persistent
	private Text description;
	@Persistent
	private String color;

	@Persistent
	private byte progress;

	@Persistent
	private Long startTime = 0L;
	@Persistent 
	private Long endTime = 0L;
	@Persistent
	protected Long locationId;

	public PersistentScene(Long locationId) {
		color = "white";
		this.locationId = locationId;
	}
	
	public void setStartTime(Long startTime) {
		this.startTime = startTime;
	}
	
	public Long getStartTime() {
		return startTime;
	}

	public void setEndTime(Long endTime) {
		this.endTime = endTime;
	}

	public Long getEndTime() {
		return endTime;
	}

	public void setProgress(byte progress) {
		this.progress = progress;
	}

	public byte getProgress() {
		return progress;
	}

	public void setNotes(String notes) {
		this.notes = new Text(notes == null ? "" : notes);
	}

	public String getNotes() {
		return notes.getValue();
	}

	protected void setId(Long id) {
		this.id = id;
	}

	public Long getId() {
		return id;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getTitle() {
		return title;
	}

	public void setDescription(String description) {
		this.description = new Text(description == null ? "" : description);
	}

	public String getDescription() {
		return description.getValue();
	}

	public void setColor(String color) {
		this.color = color;
	}

	public String getColor() {
		return color;
	}
	
	public Scene toScene(Location location) {
		Scene newScene;
		try {
			newScene = new Scene(getId(), location.getId());
		} catch (NotSupportedOnClientException e) {
			// cannot happen on server
			return null;
		}
		newScene.setColor(color);
		newScene.setDescription(description != null ? description.getValue() : "");
		newScene.setEndTime(getEndTime());
		newScene.setLocation(location);
		newScene.setNotes(notes != null ? notes.getValue() : "");
		newScene.setProgress(progress);
		newScene.setStartTime(getStartTime());
		newScene.setTitle(title);
		return newScene;
	}
	
	public void fromScene(Scene scene) {
		color = scene.getColor();
		setDescription(scene.getDescription());
		endTime = scene.getEndTime();
		startTime = scene.getStartTime();
		locationId = scene.getLocationId();
		setNotes(scene.getNotes());
		progress = scene.getProgress();
		title = scene.getTitle();
	}
	
}
